package com.sxm.yiwei.activitis;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.sdk.app.PayTask;
import com.sxm.yiwei.MainActivity;
import com.sxm.yiwei.R;
import com.sxm.yiwei.demo.PayResult;
import com.sxm.yiwei.tools.ErrType;
import com.sxm.yiwei.utils.InternetAddressUtis;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.Map;

public class MineMoneyConfirm extends BaseActivity implements View.OnClickListener{
    private static final int SDK_PAY_FLAG = 1;
    private ImageView confirmback;
    private Button confirmpay;
    private MineMoney mineMoney = new MineMoney();
    private RelativeLayout vweixin;
    private RelativeLayout vzhifu;
    private TextView ZText;
    private TextView WText;
    private CheckBox zhifubao;
    private CheckBox weixin;
    private TextView booknumber;
    private int payload = 0;
    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            PayResult payresult = new PayResult((Map<String, String>)msg.obj);
            String resultStatus = payresult.getResultStatus();
            if (resultStatus.equals("9000")){
                Toast.makeText(MineMoneyConfirm.this, "支付成功", Toast.LENGTH_SHORT).show();
            }else {
            }
        }
    };
    private SharedPreferences sharedPreferences;
    private String appId;
    private String userId;
    private String token;
    private String data;
    private String money;
    private ImageView backmain;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mine_money_confirm);
        sharedPreferences = getSharedPreferences("userlogin", Context.MODE_PRIVATE);
        appId = sharedPreferences.getString("appid", "0");
        userId = sharedPreferences.getString("userId", "1");
        token = sharedPreferences.getString("token", "2");
        Intent intent = getIntent();
        money = intent.getStringExtra("money");
        initMineMoneyConfirmView();


    }

    private void initMineMoneyConfirmView() {
        backmain = (ImageView) findViewById(R.id.mineconfirm_backmain);
        confirmback = (ImageView) findViewById(R.id.mine_moneyconfirm_back);
        confirmpay = (Button) findViewById(R.id.money_confirm_paybutton);
        vweixin = (RelativeLayout) findViewById(R.id.money_vweixin);
        vzhifu = (RelativeLayout) findViewById(R.id.money_vzhifu);
        ZText = (TextView) findViewById(R.id.money_confirm_zqian);
        WText = (TextView) findViewById(R.id.money_confirm_wqian);
        zhifubao = (CheckBox) findViewById(R.id.money_zhifubao);
        weixin = (CheckBox) findViewById(R.id.money_weixinbox);
        booknumber = (TextView) findViewById(R.id.money_booknumber);// 订单金额
            backmain.setOnClickListener(this);
        // 显示订单金额
        booknumber.setText(money);
        // 设置CheckBox单选模式
        zhifubao.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    //设置支付途径订单的状态是否可见
                    vweixin.setVisibility(View.INVISIBLE);
                    vzhifu.setVisibility(View.VISIBLE);
                    ZText.setText(money);
                    payload = 1;
                    if (weixin!=null){
                        weixin.setChecked(false);
                    }
                }
            }
        });
        // 设置CheckBox单选模式
        weixin.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    //设置支付途径订单的状态是否可见
                    vweixin.setVisibility(View.VISIBLE);
                    WText.setText(money);
                    payload=2;
                    vzhifu.setVisibility(View.INVISIBLE);
                    if (zhifubao!=null){
                        zhifubao.setChecked(false);
                    }
                }
            }
        });

        // 设置点击监听
        confirmback.setOnClickListener(this);
        confirmpay.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.mine_moneyconfirm_back:
                finish();
                break;
            case R.id.money_confirm_paybutton://点击支付
                sendBook(money,"123");
                break;
            case R.id.mineconfirm_backmain:
                startActivity(new Intent(MineMoneyConfirm.this, MainActivity.class));
                AppManager.getAppManager().finishAllActivity();
                break;
        }
    }


    // 支付宝支付
    public void sendBook(String money,String url) {
        RequestParams params = new RequestParams(InternetAddressUtis.Url+InternetAddressUtis.PAY_SERVICERMONEY);
        params.setHeader("Content-Type","application/json");
        params.setHeader("token", token);
        params.setHeader("appId", appId);
        params.setHeader("userId", userId);
        final JSONObject j = new JSONObject();
        j.put("payType","02");
        j.put("total_amount", money);
        params.setBodyContent(j.toJSONString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(final String result) {

                JSONObject obj = JSON.parseObject(result);
                data = obj.getString("data");
                final Runnable runnable = new Runnable() {
                    @Override
                    public void run() {
                        PayTask payTask = new PayTask(MineMoneyConfirm.this);
                        Map<String, String> map = payTask.payV2(data, true);
                        Message msg = new Message();
                        msg.what = SDK_PAY_FLAG;
                        msg.obj = map;
                        mHandler.sendMessage(msg);

                    }
                };
                Thread payThread = new Thread(runnable);
                payThread.start();
                Toast.makeText(MineMoneyConfirm.this, "支付成功", Toast.LENGTH_SHORT).show();

            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                ErrType.errType(MineMoneyConfirm.this,ex);
            }
            @Override
            public void onCancelled(CancelledException cex) {
            }
            @Override
            public void onFinished() {

            }
        });




    }




}
